package com.xmm.view;

import java.awt.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import com.xmm.constants.CONSTANTS;
import com.xmm.model.Artist;

public class TrackInputDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8803183732204608724L;
	
	private JTextField mTitle = new JTextField();
	private JTextField mArtist = new JTextField();
	private JTextField mLength = new JTextField();
	private JTextField mRating = new JTextField();
	private JTextField mLocation = new JTextField();
	private JTextField mAblum = new JTextField();
	private JTextField mDate = new JTextField();
	private JTextField mComment = new JTextField();
	private JTextField mSized = new JTextField();
	
	private JComponent[] input = null;
	private List<JComponent> in = new ArrayList<JComponent>();
	
	private Map<String, Object> mProps = new HashMap<String, Object>();	
	
	public TrackInputDialog() {
		init();
	}
	
	private void init() {
		in.add(new JLabel("title: "));
		in.add(mTitle);
		in.add(new JLabel("artist: "));
		in.add(mArtist);
		in.add(new JLabel("length: "));
		in.add(mLength);
		in.add(new JLabel("rating: "));
		in.add(mRating);
		in.add(new JLabel("location: "));
		in.add(mLocation);
		in.add(new JLabel("ablum: "));
		in.add(mAblum);
		in.add(new JLabel("date: "));
		in.add(mDate);
		in.add(new JLabel("comment: "));
		in.add(mComment);
		in.add(new JLabel("size: "));
		in.add(mSized);
		
		input = new JComponent[in.size()];
		in.toArray(input);
		
	}
	
	public void show(Component comp) {
		JOptionPane.showMessageDialog(comp, input, "track input dialog", JOptionPane.PLAIN_MESSAGE);
	}
	
	public Map<String, Object> getAllProps() {
		mProps.put(CONSTANTS.TRACK_SIZED_NAME, getSize());
		mProps.put(CONSTANTS.TRACK_TITLE_NAME, getTitle());
		mProps.put(CONSTANTS.TRACK_ARTIST_NAME,getArtist());
		mProps.put(CONSTANTS.TRACK_LENGTH_NAME,getLength());
		mProps.put(CONSTANTS.TRACK_ABLUM_NAME, getAblum());
		mProps.put(CONSTANTS.TRACK_DATE_NAME, getDate());
		mProps.put(CONSTANTS.TRACK_COMMENT_NAME, getComment());
		mProps.put(CONSTANTS.TRACK_RATING_NAME, getRating());
		mProps.put(CONSTANTS.TRACK_LOCATION_NAME, getLocation());
		
		return mProps;
	}

	
	public String getTitle() {
		String str = mTitle.getText();
		return str;
	}
	public String getComment() {
		String str = mComment.getText();
		return str;
	}
	public String getDate() {
		String str = mDate.getText();
		return str;
	}
	public String getAblum() {
		String str = mAblum.getText();
		return str;
	}
	public String getLocation() {
		String str = mLocation.getText();
		return str;
	}
	public Artist getArtist() {
		String str = mArtist.getText();
		if(str == null) {
			return null;
		}
		return new Artist(str, null);
	}
	public long getLength() {
		String str = mLength.getText();
		if(str == null) {
			return 0;
		}
		long l = 0;
		try {
			l = Long.valueOf(str);
		} catch(NumberFormatException e) {
			l = 0;
		}
		return l;
	}
	public double getRating() {
		String str = mRating.getText();
		if(str == null) {
			return 0;
		}
		double d = 0.0;
		try {
			d = Long.valueOf(str);
		} catch(NumberFormatException e) {
			d = 0.0;
		}
		return d;
	}
	public long getSize() {
		String str = mSized.getText();
		if(str == null) {
			return 0;
		}
		long l = 0;
		try {
			l = Long.valueOf(str);
		} catch(NumberFormatException e) {
			l = 0;
		}
		return l;
	}
}
